package Controller;

import DAOs.PeasantJpaController;
import DAOs.exceptions.NonexistentEntityException;
import Entity.Peasant;
import Entity.ReforestationZone;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Esteban
 */
public class PeasantController {

    EntityManagerFactory emf;
    EntityManager em;
    PeasantJpaController peasantDao;

    public PeasantController(EntityManagerFactory emf) {
        peasantDao = new PeasantJpaController(emf);
    }

    public void createPeasant(Peasant peasant){
        peasantDao.create(peasant);
    }
    
    public void createPeasant(String name, String lastname, int cedula, String age, String profession, String nativePeasant, List<ReforestationZone> zones) {
        Peasant peasant = new Peasant(name, lastname, cedula, age, profession, nativePeasant, zones);
        peasantDao.create(peasant);
        for(ReforestationZone rz : zones){
            String discriminator = String.valueOf(rz.getLatitude()) + String.valueOf(rz.getLongitude()) + String.valueOf(rz.getZoom());
            new satellitalImageController().createSatellitalImagePng(discriminator, rz);
        }
    }

    public ArrayList<Peasant> findAllPeasants() {
        ArrayList<Peasant> peasantsList = new ArrayList<>();
        peasantsList.addAll(peasantDao.findPeasantEntities());
        return peasantsList;
    }

    public void deletePeasant(Peasant peasant) {
        try {
            peasantDao.destroy(peasant.getId());
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PeasantController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Peasant> findByName(String name) {
        ArrayList<Peasant> peasantsList = findAllPeasants();
        
        for (int i = 0; i < peasantsList.size(); i++) {
            if (!((peasantsList.get(i).getName() + peasantsList.get(i).getLastname()).contains(name))) {
                peasantsList.remove(i);
                i--;
            }
        }
        return peasantsList;
    }

    public ArrayList<Peasant> findByCedula(int cedula) {
        ArrayList<Peasant> peasantsList = findAllPeasants();
        for (int i = 0; i < peasantsList.size(); i++) {
            if (!(peasantsList.get(i).getCedula() == cedula)) {
                peasantsList.remove(i);
                i--;
            }
        }
        return peasantsList;
    }
    
     public boolean Exist(int cedula){
        ArrayList<Peasant> peasantsList = findAllPeasants();
        for (int i = 0; i < peasantsList.size(); i++) {
            if (peasantsList.get(i).getCedula() == cedula) {
                return true;
            }
        }
        return false;
    }
   
    public void updatePeasant(Peasant peasant) throws NonexistentEntityException, Exception {
        peasantDao.edit(peasant);
    }
}
